﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MyX.Core;
using MyX.DSL;
using LearningEnglish.Models;
using LearningEnglish.ViewModels;

namespace LearningEnglish.LogicDSL
{
    /// <summary>
    /// 描述了学习者背诵英语单词的规则
    /// </summary>
    public class LeanWordDSL : XIWorkDSL
    {
        public void Init()
        {
            var work = new WorkHelper();
            work.Command<CreateLearnPlan>(
                c =>
                {
                    if (VM.LearnPlans.Exists(x => x.User.ID == c.UserID
                        && x.WordBook.ID == c.WordBookID))
                    {
                        throw new Exception("已存在任务");
                    }

                    var wordBook = new WordBook(c.WordBookID);
                    var plan = new LearnPlan(c.UserID, c.WordBookID, wordBook.Words.ToList());
                });
            work.Event<LearnPlanCreated>(
                e =>
                {
                    var planRD = new LearnPlanRD() { ID = e.XSenderID };
                    planRD.User = (from p in VM.Users where p.ID == e.UserID select p).Single();
                    planRD.WordBook = (from p in VM.WordBooks where p.ID == e.WordBookID select p).Single();
                    planRD.Words.AddRange(e.Words);

                    VM.LearnPlans.Add(planRD);
                }, true);

            work.Command<CreateLearnPlanTask>(
                c =>
                {
                    var plan = new LearnPlan(c.ID);
                    plan.Task(c.LearnDate);
                });

            work.Event<LearnPlanTaskCreated>(
                e =>
                {


                }, true);

            work.Command<ReadingWord>(
                c =>
                {
                    var plan = new LearnPlan(c.LearnPlanID);
                    plan.Read(c.Word);
                });

            work.Event<LearnPlanWordReaded>(
                e =>
                {

                }, true);

        }
    }

    public class CreateLearnPlan : XCommand
    {
        public Guid UserID { get; set; }
        public Guid WordBookID { get; set; }
    }

    public class CreateLearnPlanTask : XCommand
    {
        public Guid LearnPlanID { get; set; }
        public DateTime LearnDate { get; set; }
    }

    public class ReadingWord : XCommand
    {
        public Guid LearnPlanID { get; set; }
        public string Word { get; set; }
    }

    public class NoReadingWord : XCommand
    {
        public Guid LearnPlanID { get; set; }
        public string Word { get; set; }
    }
}